<?php

namespace app\api\controller\web;


use addons\epay\library\Service;
use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Roomclean;
use app\admin\model\Shopcharge;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptuan;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\common\controller\Api;
use Yansongda\Pay\Pay;

/**
 *
 */
class Tuan extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function getList(){

        $user = $this->auth->getUser();
        $orderNo = $this->request->param('orderNo');
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');
        $status = $this->request->param('status');

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();
        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $shop_id = $this->request->param('shop_id');
        $tuanOrderModel = new Tuanorder();
        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $where = array();
        $where2= array();
        if($shop_id != -1){
            $where['shop_id'] = $shop_id;
            $where2['shop_id'] = $shop_id;
        }


        if($startTime != ''){

            $where['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
            $where2['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
        }
        if($orderNo != ''){
            $where['order_no'] = ['like','%'.$orderNo.'%'];
            $where2['order_no'] = ['like','%'.$orderNo.'%'];
        }

        if($status != -1){
            $where['Tuanorder.status'] = array('=',$status);
            $where2['status'] =  array('=',$status);

        }

        $where['Tuanorder.org_id'] = $org['id'];
        $where2['org_id'] =  $org['id'];

        $list =  $tuanOrderModel->with(['user','shop'])->where('Tuanorder.status',"<>",'paying')->where($where)->order('create_time','desc')->limit($offset,$limit)->select();
        $total =  $tuanOrderModel->where('status',"<>",'paying')->where($where2)->count('id');
        $totalAmount =  $tuanOrderModel->where('status',"<>",'paying')->where($where2)->sum('amount');

        foreach ($list as $key=>$val){

            $val['user']->hidden(['password', 'salt']);
            $list[$key]['user_mobile'] = $val['user']['mobile'];
            $list[$key]['create_time_txt'] = date("Y-m-d h:i:s",$val['create_time']);
            $list[$key]['shop_name'] = $val['shop']['name'];

        }
        $ret = array();
        $ret['list'] = $list;
        $ret['total'] = $total;
        $ret['totalAmount'] = $totalAmount;

        $this->success('',$ret);
    }

}
